import mysql.connector
from fastapi import HTTPException
from mysql.connector import Error


def get_db_connection():
    try:
        connection = mysql.connector.connect(
            host="localhost",
            database="SomeBank",
            user="root",
            password="yelsjdhl",
        )
        if connection.is_connected():
            return connection
    except Error as e:
        print(f"Error while connecting to MySQL: {e}")
        return None


def get_db():
    conn = get_db_connection()
    if conn is None:
        raise HTTPException(status_code=503, detail="Database connection failed")
    try:
        yield conn
    finally:
        if conn.is_connected():
            conn.close()
